package com.bdqn.ch02;

import com.bdqn.ch02.pojo.Role;
import com.bdqn.ch02.pojo.SysUser;
import com.bdqn.ch02.repository.RoleRepository;
import com.bdqn.ch02.repository.SysUserRepository;
import com.bdqn.ch02.vo.RoleInfo;
import com.bdqn.ch02.vo.SysUserInfo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RoleApplcationTrsts {

    @Autowired
    private RoleRepository roleRepository;

    @Autowired
    private SysUserRepository sysUserRepository;

    @Test
    public void testGet(){
        SysUser sysUser = sysUserRepository.findById(2L).get();
        System.out.println("usrName："+sysUser.getUsrName());
        System.out.println("roleName："+sysUser.getRole().getRoleName());
    }

    @Test
    public void testGete(){//测试按主键查询角色，并输出关联的用户数量
        RoleInfo role = roleRepository.findById(1L).get();
        System.out.println("roleName："+role.getRoleName());
        System.out.println("users.size："+role.getUsers().size());
    }
    @Test
    public void testAdd(){
        RoleInfo role = new RoleInfo("角色测试","演示级联新增角色和用户",1);
        SysUserInfo user1 = new SysUserInfo("测试用户 1","123456",role,1);
        SysUserInfo user2 = new SysUserInfo("测试用户 2","123456",role,1);
        //将 SysUserInfo 添加到 RoleInfo 的 users 集合中，进行级联新增
        role.getUsers().add(user1);
        role.getUsers().add(user2);
        roleRepository.save(role);//新增角色的同时新增关联的用户
    }
    @Test
    public void testDelete(){
        RoleInfo roleInfo = roleRepository.getOne(9L);
        roleRepository.delete(roleInfo);
    }


}
